package demo;

/**
 *  字符串相加
 */
class Solution18 {
	public String addStrings(String num1, String num2) {
		int x = num1.length() - 1;
		int y = num2.length() - 1;
		int carry = 0;
		StringBuilder result = new StringBuilder();
		while (x >= 0 || y >= 0) {
			int a = x < 0 ? 0 : num1.charAt(x) - '0';
			int b = y < 0 ? 0 : num2.charAt(y) - '0';
			int sum = a + b + carry;
			carry = sum / 10;
			result.append(sum % 10);
			x--;
			y--;
		}
		if (carry > 0) {
			result.append(carry);
		}

		return result.reverse().toString();
	}

	public static void main(String[] args) {
		System.out.println(new Solution18().addStrings("456", "77"));
	}
}

